/**
 * @param {string[]} logs
 * @return {string[]}
 */
export var reorderLogFiles = function (logs) {
  const [letterLogs, digitLogs] = [[], []]
  const sort = (a, b) => {
    let i = 0
    while (i < a.length || i < b.length) {
      if (i === a.length) return -1
      if (i === b.length) return 1
      if (a.charCodeAt(i) !== b.charCodeAt(i)) {
        return a.charCodeAt(i) - b.charCodeAt(i)
      }
      i++
    }
    return 0
  }
  for (const log of logs) {
    if (log.split(' ')[1].match(/^[0-9]/)) {
      digitLogs.push(log)
    } else {
      letterLogs.push(log)
    }
  }
  letterLogs.sort((a, b) => {
    const sortContent = sort(a.slice(a.indexOf(' ') + 1), b.slice(b.indexOf(' ') + 1))
    if (sortContent === 0) {
      return sort(a.slice(0, a.indexOf(' ')), b.slice(0, b.indexOf(' ')))
    }
    return sortContent
  })
  return [...letterLogs, ...digitLogs]
}
